package priv.pfz.basedao.jdbctemplate.sqlprovider;

import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import priv.pfz.basedao.Utils;
import priv.pfz.basedao.info.DaoInfo;
import priv.pfz.basedao.jdbctemplate.SqlResult;
import priv.pfz.basedao.sharding.ShardingInfo;
import priv.pfz.basedao.sharding.ShardingManager;
import priv.pfz.basedao.sqlbuilder.SQL;

import java.util.List;

/**
 * SELECT a,b,c FROM table WHERE id in ids
 * @author yyjxp
 * 2024/1/28 23:55
 */
public class ListByIdsSqlProvider extends BaseSqlProvider {

    public static SqlResult provideSql(Iterable<Long> ids, ShardingInfo shardingInfo, DaoInfo daoInfo) {
        Utils.checkNotEmpty(ids, "ids不能为空");
        ShardingManager shardingManager = new ShardingManager(daoInfo, shardingInfo);
        List<Object> argList = Lists.newArrayList();

        SQL sql = new SQL();
        sql.SELECT(daoInfo.getSelectColumns());
        sql.FROM(shardingManager.getShardingTable());
        sql.WHERE(getInList("id", ids));
        Iterables.addAll(argList, ids);
        if (daoInfo.isEnableLogicDelete()) {
            sql.WHERE(getLogicDeleteWhere(daoInfo));
        }
        String sqlStr = sql.toString();
        sqlStr += shardingManager.getThirdShardingStr();

        System.out.println(sqlStr);
        System.out.println(argList);
        return new SqlResult(sqlStr, argList);
    }
}
